<template>
    <div>
        <!-- 头部导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>扩展</el-breadcrumb-item>
      <el-breadcrumb-item>轮播图添加</el-breadcrumb-item>
    </el-breadcrumb>
        <el-form
            :model="carouselform"
            :rules="rules"
            ref="carouselform"
            label-width="100px"
            class="demo-carouselform"
            style="margin-top:20px"
        >
            <el-form-item label="超链接到" prop="linkto">
                <el-input v-model="carouselform.linkto"></el-input>
            </el-form-item>
            <el-form-item label="显示顺序" prop="name">
                <el-input-number
                    v-model="carouselform.showorder"
                    :min="1"
                    label="描述文字"
                ></el-input-number>
            </el-form-item>

            <el-form-item label="是否显示" prop="isshow">
                <el-switch v-model="carouselform.isshow"></el-switch>
            </el-form-item>
            <el-form-item label="上传图片" prop="isshow">
                <el-upload
                    class="avatar-uploader"
                    action="/files"
                    :show-file-list="false"
                    :on-success="handleSuccess"
                    :on-remove="handleRemove"
                >
                    <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('carouselform')"
                    >添加</el-button
                >
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import axios from "axios";
export default {
    data() {
        return {
            carouselform: {
                linkto: "",
                localsrc: "",
                isshow: true,
                showorder: 1
            },
            rules: {
                linkto: [
                    {
                        required: true,
                        message: "请输入链接地址例如www.baidu.com",
                        trigger: "blur"
                    }
                ]
            },
            imageUrl: ""
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    axios.post("/carousel", this.carouselform).then(res => {
                        if (res.data) {
                            this.$message({
                                message: "上传成功",
                                type: "success"
                            });
                            this.resetForm(formName);//上传成功清空表单
                            this.imageUrl = "";
                            console.log(this.carouselform);
                        } else {
                            this.$message.error("上传失败");
                        }
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        handleRemove(file, fileList) {
            
        },
        handleSuccess(res, file, fileList) {
            this.imageUrl = URL.createObjectURL(file.raw);
            this.carouselform.localsrc = res;

            //   console.log(file);
            //   console.log(fileList);
        }
    }
};
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 360px;
    height: 180px;
    line-height: 180px;
    text-align: center;
}
.avatar {
    width: 360px;
    height: 180px;
    display: block;
}
.el-form {
    width: 460px;
}
</style>
